﻿using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.VisualStudio.Shell;

namespace Microsoft.WowAddonStudio.Project.OptionPages
{
    [Guid(GuidStrings.AddOnStudioGeneralOptionsDialogPage)]
    public class AddOnStudioGeneralOptionsDialogPage : DialogPage
    {
        private readonly AddOnStudioGeneralOptionsControl control = new AddOnStudioGeneralOptionsControl();

        /// <summary>
        /// Gets the Window control.
        /// </summary>
        /// <value></value>
        /// <!-- Failed to insert some or all of included XML -->
        /// <include file="doc\DialogPage.uex" path="docs/doc[@for=&quot;DialogPage&quot;.Window]"/>
        /// <devdoc>
        /// The window this dialog page will use for its UI.
        /// This window handle must be constant, so if you are
        /// returning a Windows Forms control you must make sure
        /// it does not recreate its handle.  If the window object
        /// implements IComponent it will be sited by the
        /// dialog page so it can get access to global services.
        /// </devdoc>
        protected override IWin32Window Window
        {
            get { return control; }
        }


        /// <summary>
        /// Gets or sets the custom deployment location.
        /// </summary>
        /// <value>The custom deployment location.</value>
        public string CustomDeploymentLocation
        {
            get { return control.CustomDeploymentLocation; }
            set { control.CustomDeploymentLocation = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [open in editor].
        /// </summary>
        /// <value><c>true</c> if [open in editor]; otherwise, <c>false</c>.</value>
        public bool OpenInEditor
        {
            get { return control.OpenInEditor; }
            set { control.OpenInEditor = value; }
        }

        /// <summary>
        /// </summary>
        /// <param name="e"></param>
        /// <!-- Failed to insert some or all of included XML -->
        /// <include file="doc\DialogPage.uex" path="docs/doc[@for=&quot;DialogPage.OnApply&quot;]"/>
        /// <devdoc>
        /// This method is called when VS wants to save the user's
        /// changes then the dialog is dismissed.
        /// </devdoc>
        protected override void OnApply(PageApplyEventArgs e)
        {
            if (CustomDeploymentLocation != null && !Directory.Exists(CustomDeploymentLocation))
            {
                e.ApplyBehavior = ApplyKind.Cancel;
                MessageBox.Show(Window, Resources.GetString(Resources.InvalidCustomDeploymentLocationPathMessage),
                                Resources.AddOnStudioforWorldofWarcraft, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                base.OnApply(e);
            }
        }
    }
}